<?php
/**
 * Created by PhpStorm.
 * User: lengbin
 * Date: 2018/6/21
 * Time: 上午11:31
 */

namespace common\core\base;

use yii\helpers\ArrayHelper;
use yii\helpers\Url;

class Action extends \yii\base\Action
{
    /**
     *  template name
     * @var string
     */
    public $view;

    /**
     *  return params
     * @var array
     */
    public $params;

    /**
     *  model class
     * @var string
     */
    public $modelClass;

    /**
     *  scenario
     * @var string
     */
    public $scenario = 'default';

    /**
     *  call_user_func
     * @var string
     */
    public $run;

    /**
     * except request params
     * @var string / array
     */
    public $exclude = 'id';

    /**
     * return params
     *
     * @var array
     */
    public $returnParams = [];

    /**
     * redirect url
     * @var string / array
     */
    public $redirect;

    use Controller;
    use InvalidException;

    public function init()
    {
        parent::init();
        if ($this->view === null) {
            $this->view = 'edit';
        }

        if ($this->redirect === null) {
            $this->redirect = 'index';
        }

        if ($this->view instanceof \Closure) {
            $this->view = call_user_func($this->view);
        }

        if ($this->params === null) {
            $this->params = [];
        }

        if ($this->params instanceof \Closure) {
            $this->params = call_user_func($this->params, $this);
        }

        if ($this->modelClass instanceof \Closure) {
            $this->modelClass = call_user_func($this->modelClass);
        }

        if ($this->modelClass !== null && !is_object($this->modelClass)) {
            $this->modelClass = new $this->modelClass;
        }
    }

    public function run()
    {
        return call_user_func($this->run, $this);
    }

    /**
     * return data ;
     *
     * @param $model
     *
     * @return array|string
     */
    public function getData($model)
    {
        $params = [];

        if ($this->returnParams instanceof \Closure) {
            $this->returnParams = call_user_func($this->returnParams, $model);
        }
        $this->params = ArrayHelper::merge($this->params, $this->returnParams);

        // if model , is not  add
        if (!$this->issetAndEmpty($this->params, 'model')) {
            $model->scenario = $this->scenario;
            $params['model'] = $model;
        }
        // if params , is not  add, quest params
        if (!$this->issetAndEmpty($this->params, 'params')) {
            $params['params'] = $this->getQueryParams($this->exclude);
        }
        $this->params = ArrayHelper::merge($this->params, $params);
        return $this->params;
    }

    /**
     * 重写 跳转
     *
     * @return \yii\web\Response
     */
    public function redirect()
    {
        $params = $this->getQueryParams($this->exclude);
        $url = ArrayHelper::merge((array)$this->redirect, $params);
        return $this->controller->redirect(Url::to($url));
    }

    /**
     * array errors change string
     *
     * @param $model
     *
     * @return string
     */
    public function getError($model)
    {
        $errorReasons = $model->getErrors();
        $err = '';
        foreach ($errorReasons as $errorReason) {
            $err .= $errorReason[0] . '<br>';
        }
        return rtrim($err, '<br>');
    }
}